#include "def_var.h"

#ifndef _MOTOR_DEFINEED_
#define	_MOTOR_DEFINEED_

#define	UP_IN1_PORT	GPIOB
#define	UP_IN1_PIN		GPIO_PIN_13
#define	UP_IN2_PORT	GPIOB
#define	UP_IN2_PIN		GPIO_PIN_14
#define	DOWN_IN1_PORT	GPIOB
#define	DOWN_IN1_PIN		GPIO_PIN_15
#define	DOWN_IN2_PORT	GPIOA
#define	DOWN_IN2_PIN		GPIO_PIN_8

#define	FPF_IN_PORT GPIOB
#define	FPF_IN1_PIN GPIO_PIN_6
#define	FPF_IN2_PIN GPIO_PIN_5
#define	FPF_IN3_PIN GPIO_PIN_4
#define	FPF_IN4_PIN GPIO_PIN_3

#define	PG_IN1_PORT GPIOB
#define	PG_IN1_PIN GPIO_PIN_9
#define	PG_IN2_PORT GPIOB
#define	PG_IN2_PIN GPIO_PIN_8
#define	PG_IN3_PORT GPIOF
#define	PG_IN3_PIN GPIO_PIN_2
#define	PG_IN4_PORT GPIOB
#define	PG_IN4_PIN GPIO_PIN_7

#define	FPFTimer	TIM1
#define	FPFTimerIRQ	TIM1_BRK_UP_TRG_COM_IRQn
#define	FPFTimerIRQHandler	TIM1_BRK_UP_TRG_COM_IRQHandler
//#define	PGTimer	TIM1
//#define	PGTimerIRQ	TIM1_BRK_UP_TRG_COM_IRQn
//#define	PGTimerIRQHandler	TIM1_BRK_UP_TRG_COM_IRQHandler
#define	PGTimer	TIM3
#define	PGTimerIRQ	TIM3_IRQn
#define	PGTimerIRQHandler	TIM3_IRQHandler

#define	MOTOR_PHASE_MAX	8
#define	MOTOR_ACCEL_STEPS 40
#define	MOTOR_BRAKE_STEPS 40
#define	MOTORPOWERON (STEP_MOTOR_SYS_CLOCK / 500)
#define	PFMACCSTR  (STEP_MOTOR_SYS_CLOCK / 200)
#define	PFMBRKEND	 (STEP_MOTOR_SYS_CLOCK / 200)
#define	FPF_NSPD (STEP_MOTOR_SYS_CLOCK / 800)

#define	PFASTEP00  100	/* STEP  0 */
#define	PFASTEP01   95	/* STEP  1 */
#define	PFASTEP02   90	/* STEP  2 */
#define	PFASTEP03   85	/* STEP  3 */
#define	PFASTEP04   82	/* STEP  4 */
#define	PFASTEP05   78	/* STEP  5 */
#define	PFASTEP06   75	/* STEP  6 */
#define	PFASTEP07   70	/* STEP  7 */
#define	PFASTEP08   68	/* STEP  8 */
#define	PFASTEP09   65	/* STEP  9 */
#define	PFASTEP10   62	/* STEP 10 */
#define	PFASTEP11   59	/* STEP 11 */
#define	PFASTEP12   55	/* STEP 12 */
#define	PFASTEP13   52	/* STEP 13 */
#define	PFASTEP14   48	/* STEP 14 */
#define	PFASTEP15   45	/* STEP 15 */
#define	PFASTEP16   42	/* STEP 16 */
#define	PFASTEP17   40	/* STEP 17 */
#define	PFASTEP18   38	/* STEP 18 */
#define	PFASTEP19   35	/* STEP 19 */
#define	PFASTEP20   32	/* STEP 20 */
#define	PFASTEP21   29	/* STEP 21 */
#define	PFASTEP22   26	/* STEP 22 */
#define	PFASTEP23   23	/* STEP 23 */
#define	PFASTEP24   20	/* STEP 24 */
#define	PFASTEP25   17	/* STEP 25 */
#define	PFASTEP26   15	/* STEP 26 */
#define	PFASTEP27   12	/* STEP 27 */
#define	PFASTEP28   9	/* STEP 28 */
#define	PFASTEP29    8	/* STEP 29 */
#define	PFASTEP30    7	/* STEP 30 */
#define	PFASTEP31    6	/* STEP 31 */
#define	PFASTEP32    5	/* STEP 32 */
#define	PFASTEP33    4	/* STEP 33 */
#define	PFASTEP34    3	/* STEP 34 */
#define	PFASTEP35    2	/* STEP 35 */
#define	PFASTEP36    1	/* STEP 36 */
#define	PFASTEP37    1	/* STEP 37 */
#define	PFASTEP38    1	/* STEP 38 */
#define	PFASTEP39    0	/* STEP 39 */

#define	PFASTEPBS	100	/* BASE   */

#define	PFACCEL40STEP( PPS_SPD )	\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP00+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP01+PPS_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ASTEP23+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP24+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP25+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP26+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP27+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP28+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP29+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP30+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP31+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP32+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP33+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP34+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP35+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP36+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP37+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP38+PPS_SPD,\
	(PFMACCSTR-PPS_SPD)/PFASTEPBS*PFASTEP39+PPS_SPD

const U16 FPFmAccelTbl[MOTOR_ACCEL_STEPS + 2] = {
	MOTOR_ACCEL_STEPS,
	PFACCEL40STEP(FPF_NSPD),
	0
};

/*
*********************************************************************
		BRAKE STEP
*********************************************************************
*/
#define	PFBSTEP00    0		/* STEP  0 */
#define	PFBSTEP01    0		/* STEP  1 */
#define	PFBSTEP02    1		/* STEP  2 */
#define	PFBSTEP03    1		/* STEP  3 */
#define	PFBSTEP04    1		/* STEP  4 */
#define	PFBSTEP05    1		/* STEP  5 */
#define	PFBSTEP06    2		/* STEP  6 */
#define	PFBSTEP07    2		/* STEP  7 */
#define	PFBSTEP08    2		/* STEP  8 */
#define	PFBSTEP09    2		/* STEP  9 */
#define	PFBSTEP10    3		/* STEP 10 */
#define	PFBSTEP11    3		/* STEP 11 */
#define	PFBSTEP12    3		/* STEP 12 */
#define	PFBSTEP13    4		/* STEP 13 */
#define	PFBSTEP14    4		/* STEP 14 */
#define	PFBSTEP15    5		/* STEP 15 */
#define	PFBSTEP16    5		/* STEP 16 */
#define	PFBSTEP17    6		/* STEP 17 */
#define	PFBSTEP18    7		/* STEP 18 */
#define	PFBSTEP19    8		/* STEP 19 */
#define	PFBSTEP20    9		/* STEP 20 */
#define	PFBSTEP21   10		/* STEP 21 */
#define	PFBSTEP22   11		/* STEP 22 */
#define	PFBSTEP23   13		/* STEP 23 */
#define	PFBSTEP24   15		/* STEP 24 */
#define	PFBSTEP25   17		/* STEP 25 */
#define	PFBSTEP26   20		/* STEP 26 */
#define	PFBSTEP27   22		/* STEP 27 */
#define	PFBSTEP28   25		/* STEP 28 */
#define	PFBSTEP29   28		/* STEP 29 */
#define	PFBSTEP30   31		/* STEP 30 */
#define	PFBSTEP31   35		/* STEP 31 */
#define	PFBSTEP32   40		/* STEP 32 */
#define	PFBSTEP33   45		/* STEP 33 */
#define	PFBSTEP34   51		/* STEP 34 */
#define	PFBSTEP35   58		/* STEP 35 */
#define	PFBSTEP36   66		/* STEP 36 */
#define	PFBSTEP37   75		/* STEP 37 */
#define	PFBSTEP38   85		/* STEP 38 */
#define	PFBSTEP39  100		/* STEP 39 */
#define	PFBSTEPBS	100	/* BASE   */

#define	PFBRAKE40STEP( PPS_SPD )	\
	(PFMBRKEND-PPS_SPD)/PFBSTEPBS*PFBSTEP00+PPS_SPD,\
	(PFMBRKEND-PPS_SPD)/PFBSTEPBS*PFBSTEP01+PPS_SPD,\
	(PFMBRKEND-PPS_SPD)/PFBSTEPBS*PFBSTEP02+PPS_SPD,\
	(PFMBRKEND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